package com.tanhua.server.controller;

import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolderThreadLocal;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import com.tanhua.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/27 20:05
 */

@RestController
@RequestMapping("/user")
public class UserController {

   @Autowired
   private UserService userService;

   @Autowired
   private UserInfoService userInfoService;

    /**
     * 获取验证码
     * @param map
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String, String> map){
        // 1. 获取手机号
        String phone = map.get("phone");
        // 2. 发送验证码
       userService.sendCode(phone);
       // 3. 返回数据
        return ResponseEntity.ok(null);
    }

    /**
     * 验证码校验
     * @param map
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> map){
        // 1. 获取集合参数
        String phone = map.get("phone");
        String code = map.get("verificationCode");
        // 2. 调用userService完成登录,返回值有token和isNew，封装到map
        Map retMap = userService.loginVerification(phone, code);
        // 3. 返回结果
        return ResponseEntity.ok(retMap);
    }

    /**
     *  登录--完善资料
     * @param userInfo
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){
        //  判断token是否合法
       /* boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            return ResponseEntity.status(401).body("用户未登录");
        }

        // 从token中获取id
        Claims claims = JwtUtils.getClaims(token);

        Integer id = (Integer) claims.get("id");*/

        Long id = UserHolderThreadLocal.getId();
        userInfo.setId(Long.valueOf(id));
        userInfoService.save(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 完善头像
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head( MultipartFile headPhoto) throws IOException {
        // 检查token
       /* boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            return ResponseEntity.status(401).body(null);
        }

        // 获取id, 手机号
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/

        Long id = UserHolderThreadLocal.getId();
        // 调用service
        userInfoService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);
    }
}
